package com.mzj.saas.mservice.ad.service;

import com.alibaba.fastjson.JSON;
import com.mzj.saas.commons.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class AdService {

    @Autowired
    @Qualifier("stringRedisTemplate9")
    private StringRedisTemplate redisTemplate9;

    @Autowired
    @Qualifier("stringRedisTemplate")
    private StringRedisTemplate redisTemplate;

    public ResultBean<Object> getAd(String space) {
        Object object = redisTemplate9.opsForValue().get("saas:advertisement:" + space);

        if (object != null) {
            List<Map<String, Object>> list = JSON.parseObject(object.toString(), List.class);
            return ResultBean.successfulResult(list);
        }

        return ResultBean.successfulResult(Collections.emptyList());
    }


    public ResultBean<Object> getAdByCode(String code) {
        Object object = redisTemplate.opsForValue().get("saas:advertisement:" + code);

        if (object != null) {
            List<Map<String, Object>> list = JSON.parseObject(object.toString(), List.class);
            return ResultBean.successfulResult(list);
        }

        return ResultBean.successfulResult(Collections.emptyList());
    }


}
